package org.liuwei.concucrent.base03.RentrantLock02;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @Title: ReentrantLockRunnable.java
 * @Package org.liuwei.concucrent.base03.ReentrantLock01
 * @Description: TODO(可中断)
 * @author 刘伟 15818570028@163.com
 * @date 2016年8月20日 下午7:36:30
 * @version V1.0
 */
public class ReentrantLockInt02 implements Runnable {
	public static ReentrantLock lock1 = new ReentrantLock();
	public static ReentrantLock lock2 = new ReentrantLock();
	int count = 0;

	public ReentrantLockInt02(int count) {
		this.count = count;
	}

	public void run() {
		try {
			if (count == 1) {
				try {
					lock1.lockInterruptibly();
					Thread.sleep(1000);
					lock2.lockInterruptibly();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else{
				try {
					lock2.lockInterruptibly();
					Thread.sleep(1000);
					lock1.lockInterruptibly();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(lock1.isHeldByCurrentThread()){
				
				lock1.unlock();
				System.out.println("lock1锁已经释放！");
			}
			if(lock2.isHeldByCurrentThread()){
				lock2.unlock();
				System.out.println("lock2锁已经释放！");
			}
		}


	}

	public static void main(String[] args) throws InterruptedException {
		ReentrantLockInt02 r = new ReentrantLockInt02(1);
		ReentrantLockInt02 r2 = new ReentrantLockInt02(2);
		Thread t = new Thread(r);
		Thread t2 = new Thread(r2);
		t.start();t2.start();
		Thread.sleep(2000);
//		DeadLock.checkAndInterrupted();


	}

}
